package com.tools.project.demo;


import at.syntaxerror.json5.JSONArray;
import at.syntaxerror.json5.JSONObject;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextArea;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * Created by 2025/5/26
 *
 * @author ldh
 * @version V1.0
 * @description: 待补充...
 */
public class GoodsVersionConvert extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        AnchorPane root = new AnchorPane();
        ObservableList<Node> children = root.getChildren();
        root.setStyle("-fx-background-color:WHITE");

        JFXTextArea url = new JFXTextArea();
        url.setLabelFloat(true);
        url.setPromptText("需要转换的数据");
        url.setMinWidth(600);
        url.setLayoutX(50);
        url.setLayoutY(150);
        url.setMinHeight(200);
        url.setMaxHeight(200);
        url.setFont(new Font(20));
        url.setWrapText(true);
        children.add(url);

        JFXTextArea result = new JFXTextArea();
        result.setLabelFloat(true);
        result.setPromptText("执行结果");
        result.setMinWidth(600);
        result.setWrapText(true);
        result.setFont(new Font(20));
        result.setLayoutX(50);
        result.setLayoutY(400);
        result.setMinHeight(200);
        result.setMaxHeight(200);
        children.add(result);

        JFXTextArea env = new JFXTextArea();
        env.setLabelFloat(true);
        env.setPromptText("版本号");
        env.setLayoutX(200);
        env.setLayoutY(700);
        env.setText("20250526");


        children.add(env);


        JFXButton button = new JFXButton("转换");
        button.setStyle("-fx-background-color:GREY");
        button.setLayoutX(400);
        button.setLayoutY(700);
        children.add(button);

        button.addEventHandler(MouseEvent.MOUSE_PRESSED, mouseEvent -> {
            if (MouseEvent.MOUSE_PRESSED.equals(mouseEvent.getEventType())) {
                String text = url.getText();

                if (text != null && url.getText() != null) {
                    JSONArray jsonArray = new JSONArray(url.getText());

                    for (Object o : jsonArray) {
                        JSONObject jsonObject = (JSONObject) o;
                        Object version = jsonObject.get("fieldVersion");
                        if (version == null) {
                            result.setText("失败");
                            break;
                        } else {
                            jsonObject.set("fieldVersion", env.getText());
                        }
                    }
                    result.setText(jsonArray.toString());

                } else {
                    result.setText("失败");
                }

            }
        });


        // 创建一个场景
        final Scene scene = new Scene(root, 1000, 800);
        scene.getStylesheets().add(UrlConvert.class.getResource("/css/jfoenix-components.css").toExternalForm());

        stage.setTitle("按钮");
        stage.setScene(scene);
        stage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}
